package mutex

type Mutex struct {
	ch	   chan bool
	isLock bool
	length int
	// WaitForComplete func (this *Mutex)() =this.Lock
}

func New(length ...int) *Mutex {
	m := new(Mutex)
	// fmt.Println(len(length))
	if len(length) == 0 {
		m.ch = make(chan bool, 0)
	} else {
		m.ch = make(chan bool, length[0])
	}
	// go func() { <-m.ch }()
	return m
}

// var E func(...interface{}) =
func (this *Mutex) WaitForComplete() { this.Lock() }
func (this *Mutex) Lock() {
	// fmt.Println(`will lock`)
	this.ch <- false
	// fmt.Println(`locked`)
	this.isLock = true
}

func (this *Mutex) SentCompleteSingnal() { this.UnLock() }
func (this *Mutex) UnLock() {
	go func() { this.isLock = <-this.ch }()
	// fmt.Println(`will un lock`)
}

func (this *Mutex) Status() (bool, int) {
	return this.isLock, this.length
}
func (this *Mutex) IsLocked() bool {
	return this.isLock
}
func (this *Mutex) Len() int {
	return this.length
}

func (this *Mutex) LockDo(function func()) {
	// for i := 0; i < gconv.Int(count); i++ {
	this.Lock()
	defer this.UnLock()
	function()

	// }

}
